﻿using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.SystemIni;
using Catastrophe.BasePC.UI.UIMode;
using Catastrophe.BasePC.UI.Model;
using Catastrophe.Common.ObjM;
using Catastrophe.Initialize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Catastrophe.BasePC.UI.UI
{
    /// <summary>
    /// 登录
    /// </summary>
    public partial class Load : Window
    {
        /// <summary>
        /// 
        /// </summary>
        public LoadModel mode { get; set;}

        /// <summary>
        /// 初始化
        /// </summary>
        private CatastropheIni cIni { get; set;}

        /// <summary>
        /// 
        /// </summary>
        private bool loadErro {  get; set;}

        /// <summary>
        /// 按键按下
        /// </summary>
        private bool keyDown {  get; set;}

        /// <summary>
        /// load
        /// </summary>
        public event Action<Window, SysUserEntity,bool> CloseAction;
        public Load()
        {
            InitializeComponent();
            mode = this.DataContext as LoadModel;
            cIni = new CatastropheIni();
            cIni.meeage += CIni_meeage;
            cIni.erroMeeage += CIni_erroMeeage;
            mode.LoginHandler += Mode_LoginHandler;
            this.Loaded += Load_Loaded;
        }

        private void Mode_LoginHandler(SysUserEntity arg1, bool arg2)
        {
            CloseAction?.Invoke(this, arg1, arg2);
            //Ggo<List<SysMenuEntity>>.GetObj = new SysMenuDal().QueryBaseMenuNode(FrameEnvironment.UserName, FrameEnvironment.UserNo, FrameEnvironment.Depart, FrameEnvironment.Identify, FrameEnvironment.AuthorizeType, FrameEnvironment.IsAdmin).ToList();
           // this.Hide();
        }

        private void CIni_erroMeeage(string obj)
        {
            loadErro=true;
            mode.LoadInfo = obj;
        }

        private void CIni_meeage(string obj)
        {
            if (loadErro==false)
            {
                mode.LoadInfo = obj;
            }
        }

        private void Load_Loaded(object sender, RoutedEventArgs e)
        {
            cIni.AllIni();
        }

        public void SetDefaultLoginInfo(string defaultUserName, string defaultPwd)
        {
            mode.UserName= defaultUserName;
            mode.Password= defaultPwd;
        }

        void Close(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(sender as FrameworkElement).Close();
            System.Environment.Exit(0);
        }

        private void DockPanel_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            keyDown = true;
        }

        private void DockPanel_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            keyDown = false;
        }

        private void DockPanel_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed && keyDown)
            {
                try
                {
                    DragMove();
                }
                catch (Exception ex) { }
            }
        }
    }
}
